<?php
class Model_Behavior_Formatter extends QDB_ActiveRecord_Behavior_Abstract
{
    protected $_settings = array
    (
//        'props' => array(),
    );

    function __construct(QDB_ActiveRecord_Meta $meta, array $settings)
    {
        parent::__construct($meta, $settings);
        $this->_settings = array_merge($this->_settings,$settings);
        if(empty($this->_settings) || ! is_array($this->_settings))
        {
            throw new QException(__("Missing \"props\" parameter in \"%s\" Model", $meta->class_name));
        }

        foreach($this->_settings as $prop => & $_config)
        {
            $_config['formatter']= isset($_config['formatter'])?$_config['formatter']:$_config[0];//兼容兩種格式參數
            $_config['mapping_name']  = isset($_config[2])?$_config[2]:null;//兼容兩種格式參數
            
            if(! isset($_config['mapping_name']))
            {
                $_config['mapping_name'] = "{$prop}_formatted";
            }
            if(isset($this->_meta->prop[$_config['mapping_name']]))
            {
                throw new QException(__("\"%s\" prop has been set up, you must use another mapping name", $_config['mapping_name']));
            }
            if(! isset($_config['formatter']))
            {
                $_config['formatter'] = "serialize";
            }
            $_config['prop_name'] = $prop;

            $this->_meta->setPropGetter(
                $_config['mapping_name'],
                array("Model_Behavior_Formatter_{$_config['formatter']}", 'get'),
                $_config
            );

            $this->_meta->setPropSetter(
                $_config['mapping_name'],
                array("Model_Behavior_Formatter_{$_config['formatter']}", 'set'),
                $_config
            );
        }
    }

    function bind()
    {
    }


}